package com.fitness.front.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fitness.front.convertor.CourseCategoryConvertor;
import com.fitness.front.modal.course.dto.CourseCategoryDto;
import com.fitness.front.modal.course.entity.CourseCategory;
import com.fitness.front.modal.course.params.CourseCategoryPageQueryParams;
import com.fitness.front.modal.course.vo.CourseCategoryVO;
import com.fitness.front.modal.response.ServerResponseEntity;
import com.fitness.front.service.ICourseCategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/course-category")
public class CourseCategoryController {

    private final ICourseCategoryService courseCategoryService;
    private final CourseCategoryConvertor courseCategoryConvertor;

    /**
     * 分页获取
     */
    @GetMapping("/page")
    public ServerResponseEntity<IPage<CourseCategoryVO>> page(CourseCategoryPageQueryParams queryParams) {
        Page<CourseCategoryVO> result = courseCategoryService.getCourseCategoryPage(queryParams);
        return ServerResponseEntity.success(result);
    }

    /**
     * 新增
     * @param equipmentDto
     * @return
     */
    @PostMapping("/add")
    public ServerResponseEntity<Boolean> add(@RequestBody CourseCategoryDto equipmentDto) {
        boolean result = courseCategoryService.addCourseCategory(equipmentDto);

        if(result) {
            return ServerResponseEntity.success();
        }
        return ServerResponseEntity.fail("C10001","添加课程失败");
    }

    /**
     * 修改
     * @param equipmentDto
     * @return
     */
    @PutMapping("/update")
    public ServerResponseEntity<Boolean> update(@RequestBody CourseCategoryDto equipmentDto) {
        boolean result = courseCategoryService.updateCourseCategory(equipmentDto);

        if(result) {
            return ServerResponseEntity.success();
        }
        return ServerResponseEntity.fail("C10002","更新课程信息失败");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public ServerResponseEntity<Boolean> delete(@RequestBody Long id) {
        boolean result = courseCategoryService.removeById(id);

        if(result) {
            return ServerResponseEntity.success(true);
        }
        return ServerResponseEntity.fail("C10003","删除课程信息失败");
    }

    /**
     * 获取课程表单
     */
    @GetMapping("/getCourseCategoryForm")
    public ServerResponseEntity<CourseCategoryVO> getCourseCategoryForm(Long id) {
        CourseCategory result = courseCategoryService.getById(id);
        return ServerResponseEntity.success(courseCategoryConvertor.entity2Vo(result));
    }

    /**
     * 获取所有分类
     */
    @GetMapping("/getAllCategory")
    public ServerResponseEntity<List<CourseCategoryVO>> getAllCategory() {
        List<CourseCategory> result = courseCategoryService.list();
        return ServerResponseEntity.success(courseCategoryConvertor.entities2Vo(result));
    }

}
